package com.partsoft.webs.utils;

import java.nio.charset.Charset;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.partsoft.utils.StringUtils;

public abstract class WebClientUtils {
	
	public static HttpServletRequest getServletRequest() {
		return RequestContextHolder.getRequestAttributes() != null ? ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest() : null;
	}

	public static String getClientRemoteAddr() {
		HttpServletRequest request = getServletRequest();
		String clientIp = request.getHeader("HTTP_X_FORWARD_FOR");
		if (!StringUtils.hasText(clientIp)) {
			clientIp = request.getHeader("X-Real-IP");
		}
		if (!StringUtils.hasText(clientIp)) {
			clientIp = request.getRemoteAddr();
		}
		return clientIp;
	}
	
	public static void setupClientInfo(String orientation, int deskWidth, int deskHeight, int scWidth, int scHeight,
			int colorDepth) {
		if (RequestContextHolder.getRequestAttributes() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession() != null) {
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
					.getSession();
			session.setAttribute("__zk_desktop_orientation__", orientation);
			session.setAttribute("__zk_desktop_width__", deskWidth);
			session.setAttribute("__zk_desktop_height__", deskHeight);
			session.setAttribute("__zk_screen_width__", scWidth);
			session.setAttribute("__zk_screen_height__", scHeight);
			session.setAttribute("__zk_color_depth__", colorDepth);
		}
	}
	
	public static String getClientDesktopOrientation() {
		String result = null;
		if (RequestContextHolder.getRequestAttributes() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession() != null) {
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
					.getSession();
			result = session.getAttribute("__zk_desktop_orientation__").toString();
		}
		return result;
	}
	
	public static int getClientDesktopWidth() {
		int result = -1;
		if (RequestContextHolder.getRequestAttributes() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession() != null) {
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
					.getSession();
			result = (Integer)session.getAttribute("__zk_desktop_width__");
		}
		return result;
	}
	
	public static int getClientDesktopHeight() {
		int result = -1;
		if (RequestContextHolder.getRequestAttributes() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest() != null
				&& ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession() != null) {
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
					.getSession();
			result = (Integer)session.getAttribute("__zk_desktop_height__");
		}
		return result;
	}
	

	public static ResponseEntity<String> buildResponse(String bodyMsg, Charset charset, HttpStatus status) {
		HttpHeaders headers = new HttpHeaders();
		MediaType mt = new MediaType("text", "html", charset);
		headers.setContentType(mt);
		return new ResponseEntity<String>(bodyMsg, headers, status);
	}

	public static ResponseEntity<String> buildResponse(String bodyMsg, HttpStatus status) {
		return buildResponse(bodyMsg, Charset.forName("utf-8"), status);
	}

	public static ResponseEntity<String> buildResponse(String bodyMsg) {
		return buildResponse(bodyMsg, Charset.forName("utf-8"), HttpStatus.OK);
	}
	
}
